package Lotr.game.Entity;

import Lotr.game.GameManager;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class Spell {

    protected GameManager gameManager;
    protected boolean chosen, available, visible, casted;
    protected int cooldown, cooldownLimit;
    protected int x, y, width, height;

    public void update() {
        if (cooldown == 0) {
            available = true;
            if (casted) {
                visible = false;
                casted = false;
            }
        }
        if (!available) {
            cooldown--;
        }
        Point mousePos = gameManager.getMousePosition();
        if (chosen) {
            if (mousePos != null) {
                x = mousePos.x - width / 2;
                y = mousePos.y - height / 2;
            }
        }
    }

    public void render(Graphics g) {
        if (!visible) {
            return;
        }
        if (chosen) {
            g.setColor(Color.RED);
            g.drawOval(x, y, width, height);
        } else if (casted) {
            g.setColor(Color.GREEN);
            g.fillOval(x, y, width, height);
        }
    }

    public void spellChosen() {
        if (!available) {
            return;
        }
        if (chosen) {
            spellAborted();
            return;
        }
        chosen = true;
        visible = true;
    }

    public void spellCasted() {
        if (!available) {
            return;
        }
        if (!chosen) {
            return;
        }
        chosen = false;
        casted = true;
        available = false;
        cooldown = cooldownLimit;
        Point mousePos = gameManager.getMousePosition();
        if (mousePos != null) {
            x = mousePos.x - width / 2;
            y = mousePos.y - height / 2;
        }
    }

    public void spellAborted() {
        chosen = false;
        visible = false;
    }
}
